<template>
  <div class="box_userpwd">
    <div id="box2">
      <div id="updatepwd">
        <div id="yuanzhu"></div>
          <p>重置密码</p>
      </div>
      <div class="udPwd">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="旧密码" prop="oldPwd">
            <el-input v-model.number="ruleForm.oldPwd"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPwd">
            <el-input type="password" v-model="ruleForm.newPwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPwd">
            <el-input type="password" v-model="ruleForm.checkPwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')" style="margin-left:100px">提交</el-button>
            <el-button @click="resetForm('ruleForm')" style="margin-left:30px">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
axios.defaults.baseURL = "http://localhost:8080"
export default {
  data() {
      var validateOldPwd = (rule, value, callback) => {
        let loginPwd=window.sessionStorage.getItem('loginPwd')
        if(value===''){
          callback(new Error('请输入密码'));
        }else if(value!=loginPwd){
          callback(new Error('与原密码不同'));
        }else if(value==loginPwd){
          callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPwd !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.newPwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          oldPwd: '',
          newPwd: '',
          checkPwd: ''
        },
        rules: {
          oldPwd:[
            { validator: validateOldPwd, trigger: 'blur' }
          ],
          newPwd: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPwd: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            axios({
            url:"/doctor/updatePwd",
            method:"post",
            params:{
              pwd:this.ruleForm.newPwd,
              userId:window.sessionStorage.getItem('userId')
            }
          }).then((res) =>{
            this.$message.success('修改成功')
            window.sessionStorage.setItem('loginPwd',this.ruleForm.newPwd)
          })
          } else {
            this.$message.error('请正确输入')
            return false;
          }
        });
        
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
    },
}
</script>

<style>
    .udPwd{
      width: 500px;
      margin: 100px auto;
    }

    .box_userpwd{
        width: 100%;
        height: 685px;
        background-color: #F2F7FB;
    }

    #box2{
        width: 1150px;
        height: 685px;
        margin: 20px auto;
        background: rgb(255, 255, 255);
        position: relative;
        font-family: 'PingFangHK-Medium', 'PingFang HK Medium', 'PingFang HK';
    }

    #yuanzhu{
        width: 6px;
        height: 20px;
        background-color: #2984F8;
        border-radius: 8px;
        position: absolute;
        top: 20px;
        left: 20px;
    }

    #updatepwd{
        font-size: 18px;
        line-height: 60px;
    }

    #updatepwd p{
        margin-left: 40px;
    }
</style>